package com.manager.plat.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.manager.plat.dao.ILimitUserConsumDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.LimitUserConsumVo;

public class LimitUserConsumDaoImpl extends JdbcDaoSupport implements ILimitUserConsumDao {
	private static Logger logger = Logger.getLogger(LimitUserConsumDaoImpl.class);
	
	/**获得用户总量限制列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<LimitUserConsumVo> getLimitUserConsumList() {
		String sql = "select id, sp_number, day_price, month_price, status from tb_ivr_limit_user_consum where status = 1";
		logger.info(String.format("获得自消费用户总量限制列表[%s]", sql));
		
		List<LimitUserConsumVo> limitUserConsumVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(limitUserConsumVoList != null && limitUserConsumVoList.size() > 0){
			return limitUserConsumVoList;
		}
		return null;
	}
	/**根据Id获得用户总量限制信息*/
	@SuppressWarnings("unchecked")
	@Override
	public LimitUserConsumVo getLimitUserConsumById(int id) {
		String sql = "select id, sp_number, day_price, month_price, status from tb_ivr_limit_user_consum where id = ? and status = 1";
		Object[] param = new Object[]{id};
		logger.info(String.format("根据Id获得自消费用户总量限制信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<LimitUserConsumVo> limitUserConsumVoList =this.getJdbcTemplate().query(sql, param, rowMapper);
		if(limitUserConsumVoList != null && limitUserConsumVoList.size() > 0){
			return limitUserConsumVoList.get(0);
		}
		return null;
	}
	/**数据转换*/
	private ParameterizedRowMapper<LimitUserConsumVo> rowMapper = new ParameterizedRowMapper<LimitUserConsumVo>() {
		@Override
		public LimitUserConsumVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			LimitUserConsumVo limitUserConsumVo = new LimitUserConsumVo();
			limitUserConsumVo.setId(rs.getInt("id"));
			limitUserConsumVo.setSp_number(rs.getString("sp_number"));
			limitUserConsumVo.setDay_price(rs.getInt("day_price"));
			limitUserConsumVo.setMonth_price(rs.getInt("month_price"));
			limitUserConsumVo.setStatus(rs.getInt("status"));
			return limitUserConsumVo;
		}
	};
	
	/**新增用户总量限制信息*/
	@Override
	public int insertLimitUserConsum(LimitUserConsumVo limitUserConsumVo) {
		String sql = "insert into tb_ivr_limit_user_consum(sp_number, day_price, month_price, status, add_user) values(?, ?,?,1,?)";
		Object[] param = new Object[]{limitUserConsumVo.getSp_number(), limitUserConsumVo.getDay_price(), limitUserConsumVo.getMonth_price(), limitUserConsumVo.getAdd_user()};
		logger.info(String.format("新增自消费用户总量限制信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**更新用户总量限制信息*/
	@Override
	public int updateLimitUserConsum(LimitUserConsumVo limitUserConsumVo) {
		String sql = "update tb_ivr_limit_user_consum set sp_number = ?, day_price = ?, month_price = ?, add_user = ? where id = ?";
		Object[] param = new Object[]{limitUserConsumVo.getSp_number(), limitUserConsumVo.getDay_price(), limitUserConsumVo.getMonth_price(), 
									limitUserConsumVo.getAdd_user(),limitUserConsumVo.getId()};
		logger.info(String.format("更新自消费用户总量限制信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**删除用户总量限制信息*/
	@Override
	public int deleteLimitUserConsum(int id) {
		String sql = "update tb_ivr_limit_user_consum set status = 0 where id = ?";
		Object[] param = new Object[]{id};
		logger.info(String.format("删除自消费用户总量限制信息[%s]", BaseUtil.logSQL(sql, param)));

		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
}
